package com.piggy.logi.sys.controller;

import com.piggy.common.core.domain.R;
import com.piggy.common.core.web.controller.BaseController;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.common.log.annotation.LogiLog;
import com.piggy.common.log.enums.BusinessType;
import com.piggy.common.satoken.utils.SecurityUtils;
import com.piggy.logi.sys.bo.LogiWaybillInvoiceEditBo;
import com.piggy.logi.sys.bo.LogiWaybillInvoiceQueryBo;
import com.piggy.logi.sys.bo.LogiWaybillIdsBo;
import com.piggy.logi.sys.service.ILogiWaybillInvoiceService;
import com.piggy.logi.sys.vo.LogiWaybillInvoiceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 发票管理Controller
 *
 * @author fengxy
 * @date 2023/10/17 21:28
 **/
@Api(value = "发票管理控制器", tags = {"发票管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/waybill/invoice")
public class LogiWaybillInvoiceController extends BaseController {

    private final ILogiWaybillInvoiceService iLogiWaybillInvoiceService;

    /**
     * 查询开票记录列表
     */
    @ApiOperation("查询开票记录列表")
    //@SaCheckPermission("sys:waybill:invoice:list")
    @PostMapping("/list")
    public TableDataInfo<LogiWaybillInvoiceVo> list(@Validated @RequestBody LogiWaybillInvoiceQueryBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        return iLogiWaybillInvoiceService.queryPageList(bo);
    }

    /**
     * 根据运单id获取电子发票
     */
    @ApiOperation("根据运单id获取电子发票")
    //@SaCheckPermission("sys:waybill:invoice:electronInvoice")
    @GetMapping("/electronInvoice/{waybillId}")
    public R<String> getElectronInvoice(@ApiParam(value = "运单id") @NotNull(message = "运单id不能为空") @PathVariable("waybillId") Long waybillId) {
        return R.ok(iLogiWaybillInvoiceService.getElectronInvoice(waybillId));
    }

    /**
     * 新增开票记录
     **/
    @ApiOperation("新增开票记录")
    //@SaCheckPermission("sys:waybill:invoice:add")
    @LogiLog(title = "发票管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Object> add(@Validated @RequestBody LogiWaybillInvoiceEditBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        iLogiWaybillInvoiceService.insertByAddBo(bo);
        return R.ok();
    }

    /**
     * 暂不开票
     **/
    @ApiOperation("暂不开票")
    //@SaCheckPermission("sys:waybill:invoice:noNeed")
    @LogiLog(title = "发票管理", businessType = BusinessType.UPDATE)
    @PostMapping("/noNeed")
    public R<Object> noNeed(@Validated @RequestBody LogiWaybillIdsBo bo) {
        iLogiWaybillInvoiceService.noNeed(bo);
        return R.ok();
    }

    /**
     * 恢复开票
     **/
    @ApiOperation("恢复开票")
    //@SaCheckPermission("sys:waybill:invoice:recover")
    @LogiLog(title = "发票管理", businessType = BusinessType.UPDATE)
    @PostMapping("/recover")
    public R<Object> recover(@Validated @RequestBody LogiWaybillIdsBo bo) {
        iLogiWaybillInvoiceService.recover(bo);
        return R.ok();
    }

    /**
     * 发送邮箱
     **/
    @ApiOperation("发送邮箱")
    //@SaCheckPermission("sys:waybill:invoice:reInvoice")
    @LogiLog(title = "发票管理", businessType = BusinessType.OTHER)
    @PostMapping("/sendEmail/{id}/{email}")
    public R<Object> sendEmail(@ApiParam(value = "发票id") @NotNull(message = "发票id不能为空") @PathVariable(value = "id") Long id,
                                             @ApiParam(value = "邮箱地址") @NotBlank(message = "邮箱地址不能为空") @PathVariable(value = "email") String email) {
        iLogiWaybillInvoiceService.sendEmail(id, email);
        return R.ok();
    }

    /**
     * 重新开票
     **/
    @ApiOperation("重新开票")
    //@SaCheckPermission("sys:waybill:invoice:reInvoice")
    @LogiLog(title = "发票管理", businessType = BusinessType.UPDATE)
    @PostMapping("/reInvoice")
    public R<Object> reInvoice(@Validated @RequestBody LogiWaybillInvoiceEditBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        iLogiWaybillInvoiceService.reInvoice(bo);
        return R.ok();
    }

    /**
     * 作废
     **/
    @ApiOperation("作废")
    //@SaCheckPermission("sys:waybill:invoice:obsolete")
    @LogiLog(title = "发票管理", businessType = BusinessType.UPDATE)
    @PostMapping("/obsolete/{id}")
    public R<Object> obsolete(@ApiParam(value = "发票id") @NotNull(message = "发票id不能为空") @PathVariable(value = "id") Long id) {
        iLogiWaybillInvoiceService.obsolete(id);
        return R.ok();
    }

}
